package org.ljy.forum6.common;

/*
    统一结果返回的信息类，包含状态码、返回结果的信息、想要返回的真正数据
 */


import com.fasterxml.jackson.annotation.JsonInclude;

public class AppResult<T> {

    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private T data;


    /**
     * 构造方法
     */
    public AppResult(int code, String message) {
       this(code,null,message);
    }

    public AppResult(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    /**
     * 成功的结果
     */
    public static AppResult success() {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMassage());
    }

    public static AppResult success(String message) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message);
    }
    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), data,ResultCode.SUCCESS.getMassage());
    }
    public static <T> AppResult<T> success(String message,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), data,message);
    }

    /**
     * 失败的结果
     */
    public static AppResult failed() {
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMassage());
    }

    public static AppResult failed(String message) {
        return new AppResult<>(ResultCode.FAILED.getCode(), message);
    }

    public static AppResult failed(ResultCode resultCode) {
        return new AppResult<>(resultCode.getCode(), resultCode.getMassage());
    }

    /**
     * get和set方法
     * @return
     */

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
